package com.dst.smart.tools;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.xml.sax.InputSource;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;

import com.dst.smart.app.MainActivity;
import com.dst.smart.app.MyApplication;
import com.dst.smart.json.ProjectUtils;

/**
 *
 * @author track
 * @version 1.0
 */
public class AppTools {

	public static final String ACTION = "com.dst.newsmarthome.activity.HelpCodeActivity";

	public static String getCurrentlyDate() {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(new Date());
	}

	public static String getServiceName(){
		String hostIP = ProjectUtils.getRootPath()
				.getServiceIP();
		String result="未连接服务器";
		if (hostIP.equals("103.40.22.138")) {
			result ="当前连接到1号和利扬服务器";
		} else if (hostIP.equals("60.251.48.20")) {
			result= "当前连接到2号创力特服务器";
		} else if (hostIP.equals("47.94.154.118")) {
			result= "当前连接到3号准德服务器";
		} else if (hostIP.equals("112.74.56.95")) {
			result= "当前连接到4号创先服务器";
		}
		return result;
	}

	public static InputSource getNetInputSource(String site, String format)
			throws IOException {
		site += "&random=" + Math.random();
		URL url = new URL(site);
		URLConnection conn = url.openConnection();
		InputStream is = conn.getInputStream();
		InputStreamReader isr = new InputStreamReader(is, format);
		InputSource source = new InputSource(isr);
		return source;
	}

	public static String getLocaleLanguage() {
		Locale l = Locale.getDefault();
		return String.format("%s", l.getLanguage(), l.getCountry())
				.toUpperCase();
	}

	// 此方法直接照搬自网络上的�?个下拉刷新的demo，此处是“估计�?�headView的width以及height
	public static void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
		}
		child.measure(childWidthSpec, childHeightSpec);
	}

	public static String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			ex.printStackTrace();
		}
		return null;
	}

	public static String getDateTime(String createTime) {

		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Long
				.parseLong(createTime.replace("/Date(", "").replace(")/", "")));
	}

	public static String getCurrentTime() {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return dateFormat.format(new Date());
	}

	// public static String getDateYMD(String createTime) {
	//
	// return new SimpleDateFormat("MM月dd�?").format(Long.parseLong(createTime
	// .replace("/Date(", "").replace(")/", "")));
	// }

	public static String getDate() {

		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(date);
	}

	public static String getTime() {
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("HH:mm");
		return format.format(date);
	}

	public static int getWeekOfDate() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return w;
	}
	public static String wifiName() {
		String wifiInfoName="";
		try {
			ConnectivityManager connectivityManager = (ConnectivityManager) MyApplication.getInstance()
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo activeNetworkInfo = connectivityManager
					.getActiveNetworkInfo();
			if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
				WifiManager wifiManager = (WifiManager) MyApplication.getInstance().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
				WifiInfo wifiInfo = wifiManager.getConnectionInfo();
				wifiInfoName = wifiInfo.getSSID();
			}
		}catch (Exception e){

		}
		return wifiInfoName;
	}
	public static String getAlias() {
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		return format.format(date);
	}

	private static String hexString = "0123456789ABCDEF";

	public static String toStringHex(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(
						s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			s = new String(baKeyword, "UTF-8");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

	public static boolean isWifi(Context mContext) {
		ConnectivityManager connectivityManager = (ConnectivityManager) mContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null
				&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
			return true;
		}
		return false;
	}
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}
	public static boolean isIPAddress(String ipaddr) {
		boolean flag = false;
		Pattern pattern = Pattern
				.compile("\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b");
		Matcher m = pattern.matcher(ipaddr);
		flag = m.matches();
		return flag;
	}
	public  static String getInts(int[] temp){
		String result="";

		for (int i=0;i<temp.length;i++){
			if(i==temp.length-1){
				result += temp[i];
			}else {
				result += temp[i] + ",";
			}
		}
		return result;
	}
	public static String getNumbers(String content) {
		Pattern pattern = Pattern.compile("\\d+");
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			return matcher.group(0);
		}
		return "0";
	}
	public static byte[] intToByteArray(int i) {
		byte[] result = new byte[4];
		result[0] = (byte)(i & 0xFF);
		result[1] = (byte)((i >> 8) & 0xFF);
		result[2] = (byte)((i >> 16) & 0xFF);
		result[3] = (byte)((i >> 24) & 0xFF);
		return result;
	}
}
